package com.dmxh.controller;

import com.dmxh.params.QueryParams;
import com.dmxh.result.PageList;
import com.dmxh.result.Result;
import com.dmxh.service.ItemCatService;
import com.dmxh.vo.ItemCatVo;
import com.dmxh.vo.TypeTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/itemcat")
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    @PostMapping("/queryPage")
    public Result<PageList<Map<String, Object>>> queryPage(@RequestBody QueryParams queryParams) {
        PageList<Map<String, Object>> pageList = itemCatService.queryPage(queryParams);
        return new Result<>("1", "操作成功", pageList);
    }

    @GetMapping("/queryTree")
    public Result queryTree() {
        List<Map<String, Object>> list = itemCatService.queryTree();
        return new Result<>("1", "操作成功", list);
    }

    @GetMapping("/queryById/{id}")
    public Result<ItemCatVo> queryById(@PathVariable("id") long id) {
        ItemCatVo itemCatVo = itemCatService.queryById(id);
        return new Result<>("1", "操作成功", itemCatVo);
    }

    @GetMapping("/queryTreeById/{id}")
    public Result queryTreeById(@PathVariable("id")long id) {
        List<Map<String, Object>> list = itemCatService.queryTreeById(id);
        return new Result<>("1", "操作成功", list);
    }

    @PostMapping("/save")
    public Result save(@RequestBody ItemCatVo itemCatVo) {
        //增加
        if (itemCatVo.getId() == null) {
            itemCatService.addInfo(itemCatVo);
        }
        //修改
        else {
            itemCatService.updateInfo(itemCatVo);
        }
        return Result.ok();
    }

    @DeleteMapping("/deleteInfo/{id}")
    public Result deleteInfo(@PathVariable("id") long id) {
        itemCatService.deleteInfo(id);
        return Result.ok();
    }

    @PatchMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Integer ids) {
        itemCatService.deleteBatch(ids);
        return Result.ok();
    }

    @GetMapping("/queryBrandById/{id}")
    public Result queryBrandById(@PathVariable("id")long id) {
        TypeTemplateVo typeTemplateVo = itemCatService.queryBrandById(id);
        return Result.ok().setData(typeTemplateVo);
    }
}
